const path = require('path')
const config = require('./webpack.common')
const { merge } = require('webpack-merge')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin')
const TerserWebpackPlugin = require('terser-webpack-plugin')

module.exports = merge(config, {
  mode: 'production',
  module: {
    rules: [
      {
        test: /\.(png|jpg|jpeg)$/,
        loader: 'image-webpack-loader',// 压缩图片
        options: {
          bypassOnDebug: true,
        }
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader']
      },
      {
        test: /\.less$/,
        use: ['css-loader', 'less-loader']
      },
    ]
  },
  plugins: [
    new CopyWebpackPlugin({
      patterns: [
        'public' // 将制定目录下面的文件，copy到打包生成目录中
      ]
    }),
    new CleanWebpackPlugin(),
    new MiniCssExtractPlugin()
  ],
  optimization: {
    minimizer: [
      new TerserWebpackPlugin(),
      new OptimizeCssAssetsWebpackPlugin()
    ]
  }
})